package cn.com.yusys.yusp.admin.web.rest;

import java.util.List;
import java.util.Map;

import cn.com.yusys.yusp.commons.mapper.QueryModel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.yusys.yusp.admin.service.UserProviderService;
import cn.com.yusys.yusp.commons.dto.Contr;
import cn.com.yusys.yusp.commons.dto.DataContr;
import cn.com.yusys.yusp.commons.dto.MenuContr;
import cn.com.yusys.yusp.commons.dto.User;
import cn.com.yusys.yusp.commons.service.UserService;
import cn.com.yusys.yusp.uaa.security.AuthUtils;

@RestController
@RequestMapping("/api")
public class UserProviderServiceResource {
    private static final Logger logger = LoggerFactory.getLogger(UserProviderServiceResource.class);
 
    @Autowired
    private UserService userService;
    
    @Autowired
    private UserProviderService providerService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value = "/session/info")
    public User getUserInfo() {
        Map parsedTokenMap = AuthUtils.getParsedAccessTokenMap();
        String accessToken = AuthUtils.getCurrentUserToken(parsedTokenMap);
        String sysId = AuthUtils.getCurrentUserLoginSYS(parsedTokenMap);
        if (StringUtils.isEmpty(accessToken)) {
            logger.warn("Failed to get user authorization information {}", accessToken);
            return null;
        }else {
            String loginCode = userService.getUserCode(accessToken);
            logger.info("userCode: {}", loginCode);
            return providerService.getUserInfo(loginCode, sysId);
        }
    }
    
    @GetMapping(value = "/user/usercode")
    public String getUserCode() {
        return providerService.getUserCode();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping("/account/menuandcontr")
    public MenuContr getMenuandContr(QueryModel queryModel) {
        Map parsedTokenMap = AuthUtils.getParsedAccessTokenMap();
        String loginCode = AuthUtils.getCurrentUserLogin(parsedTokenMap);
        String sysId = AuthUtils.getCurrentUserLoginSYS(parsedTokenMap);
        String roleId = queryModel.getCondition().get("roleId").toString();
        return providerService.getMenuandContr(loginCode, sysId,roleId);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping("/account/dataauth")
    public List<? extends DataContr> getDataAuth() {
        Map parsedTokenMap = AuthUtils.getParsedAccessTokenMap();
        String loginCode = AuthUtils.getCurrentUserLogin(parsedTokenMap);
        String sysId = AuthUtils.getCurrentUserLoginSYS(parsedTokenMap);
        return providerService.getDataAuth(loginCode, sysId);
    }

    @GetMapping("/contr/url")
    public List<? extends Contr> getAllContrUrl() {
        return providerService.getAllContrUrl();
    }

 
    @GetMapping("/contr/clearUrl")
    public ResponseEntity<String> clearAllContrUrl() {
    	providerService.cleanAllContrUrlCache();
    	return new ResponseEntity<>("清除成功", HttpStatus.OK);
    }
    
    //modify by dusong 20190308 由uaa提供
//	@GetMapping(value = "/logout")
//	public ResponseEntity logout() {
//		String accessToken = SecurityUtils.getCurrentUserToken();
//		userService.cleanDataContrCache(accessToken);
//		userService.cleanMenuAndContrCache(accessToken);
//		userService.cleanUserInfoCache(accessToken);
//		
//		userService.cleanAllContrUrlCache();
//		return new ResponseEntity<>(HttpStatus.OK);
//	}
//	
//	@PostMapping(value = "/logout")
//	public ResponseEntity logoutPost() {
//		String accessToken = SecurityUtils.getCurrentUserToken();
//		userService.cleanDataContrCache(accessToken);
//		userService.cleanMenuAndContrCache(accessToken);
//		userService.cleanUserInfoCache(accessToken);
//		
//		userService.cleanAllContrUrlCache();
//		return new ResponseEntity<>(HttpStatus.OK);
//	}
    
    @SuppressWarnings("rawtypes")
	@GetMapping(value = "/cleanMenuAndContrCache")
	public ResponseEntity cleanMenuAndContrCache() {
		providerService.cleanMenuAndContrCache();
		return new ResponseEntity<>(HttpStatus.OK);
	}
	
	@SuppressWarnings("rawtypes")
	@GetMapping(value = "/cleanUserInfoCache")
	public ResponseEntity cleanUserInfoCache() {
		providerService.cleanUserInfoCache();
		return new ResponseEntity<>(HttpStatus.OK);
	}
	
	@SuppressWarnings("rawtypes")
	@GetMapping(value = "/cleanDataContrCache")
	public ResponseEntity cleanDataContrCache() {
		providerService.cleanDataContrCache();
		return new ResponseEntity<>(HttpStatus.OK);
	}
	
}
